import axios from 'axios'
import {getToken} from "@/utils/token";
import qs from 'qs'

// axios实例
const service = axios.create({
    timeout: 5000,
    paramsSerializer: function (params) {
        return qs.stringify(params, {
            arrayFormat: 'brackets'
        })
    },
    baseURL: import.meta.env.VITE_APP_BASE_API,
})

// 请求的拦截器
service.interceptors.request.use(config => {
    const token = getToken();
    if (token) {
        config.headers.Authorization = `Bearer ${token}`
    }
    return config
})

// 响应拦截器
service.interceptors.response.use(
    response => {
        return response
    },
    error => {
        console.error(error)
    }
)

// 返回axios实例
export default service
